package com.maori.languageapp.curate.modifynodes;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;
import com.maori.languageapp.R;
import com.maori.languageapp.consts.UrlContant;
import com.maori.languageapp.curateword.Maori_Curate_Word_Details;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class Maori_Curate_Reject_Reson_Submit extends Activity {
	
	String IMAGENM_ID,IMAGENID,NodeApproveReject,NodeApproveRejectMedia;
	Button rejectaccept,rejectcancel;
	EditText rejectReasontitle,rejectReasonDescription;
	ProgressDialog mProgressDialog;
	int timeoutConnection = 5000;
	HttpParams httpParameters = new BasicHttpParams();
	InputStream is = null;
	StringBuilder sb = null;
	String result,NodeApproveA,NodeApproveB;
	JSONArray jsonarray;

	String saveImageNodeReject = UrlContant.saveImageNodeReject;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Remove title bar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.languageapp_curate_reject_form);
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			IMAGENM_ID = extras.getString("IMAGENM_ID");
			IMAGENID = extras.getString("IMAGENID");
			NodeApproveReject = extras.getString("NodeApproveReject");
			NodeApproveRejectMedia = extras.getString("NodeApproveRejectMedia");
		}
		
		rejectaccept = (Button) findViewById(R.id.rejectaccept);
		rejectaccept.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				rejectReasontitle = (EditText) findViewById(R.id.rejectReasontitle);
				rejectReasonDescription = (EditText) findViewById(R.id.rejectReasonDescription);
				new sendToReject().execute();
			}
		});
		
		rejectcancel = (Button) findViewById(R.id.rejectcancel);
		rejectcancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				onBackPressed();
			}
		});
	}
	
	
	/**************** Uploading Images Via FTP ************************/
	class sendToReject extends AsyncTask<Void, Void, Void> {

		private final String TAG = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			// Create a progressdialog
			mProgressDialog = new ProgressDialog(Maori_Curate_Reject_Reson_Submit.this);
			// Set progressdialog title
			// mProgressDialog.setTitle("Loading");
			// Set progressdialog message
			mProgressDialog.setMessage("Processing...");
			mProgressDialog.setIndeterminate(false);
			// Show progressdialog
			mProgressDialog.show();

		}

		@Override
		protected Void doInBackground(Void... params) {
			Intent x = getIntent();
			ArrayList<NameValuePair> nameValuePairsMainW = new ArrayList<NameValuePair>();
			try {
				HttpConnectionParams.setConnectionTimeout(httpParameters,
						timeoutConnection);
				HttpClient httpclient = new DefaultHttpClient(httpParameters);
				HttpPost httppost = new HttpPost(saveImageNodeReject);
				JSONObject json = new JSONObject();
				json.put("record0", IMAGENM_ID);
				json.put("record1", IMAGENID);
				json.put("record2", NodeApproveReject);
				json.put("record3", NodeApproveRejectMedia);
				json.put("record4", rejectReasontitle.getText().toString());
				json.put("record5", rejectReasonDescription.getText().toString());

				JSONArray postjson = new JSONArray();
				postjson.put(json);

				httppost.setHeader("json", json.toString());
				httppost.getParams().setParameter("jsonpost", postjson);
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairsMainW));
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
				is = entity.getContent();

				if (is != null) {

					BufferedReader reader = new BufferedReader(
							new InputStreamReader(is, "iso-8859-1"), 8);
					sb = new StringBuilder();
					sb.append(reader.readLine() + "\n");
					String line = "0";

					while ((line = reader.readLine()) != null) {
						sb.append(line + "\n");
					}
					is.close();
					result = sb.toString();
					if (result != null) {
						Log.e("REQUEST :", "RESULT***** =" + result);
						try {
							JSONObject jsonObj = new JSONObject(result);
							jsonarray = jsonObj.getJSONArray("posts");
							for (int i = 0; i < jsonarray.length(); i++) {
								JSONObject c = jsonarray.getJSONObject(i);
								// Get the result of rank
								NodeApproveA = c.getString("node_active_a");
								NodeApproveB = c.getString("node_active_b");
							}
						}
						catch(Exception e){
							
						}
					}
				}

			} catch (Exception e) {
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			mProgressDialog.dismiss();
			final AlertDialog alertDialog = new AlertDialog.Builder(
					Maori_Curate_Reject_Reson_Submit.this).create();
			// Setting Dialog Title
			alertDialog.setTitle("Rejected");
			// Setting Dialog Message
			alertDialog.setMessage("Word Details Rejected Successfully...");
			// Setting OK Button
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					/*if(NodeApproveA.equals("3")){
						curate_reject.setCompoundDrawablesWithIntrinsicBounds(R.drawable.b_cross, 0, 0, 0);
						curate_approve.setCompoundDrawablesWithIntrinsicBounds(R.drawable.g_tick, 0, 0, 0);
						curate_modify.setCompoundDrawablesWithIntrinsicBounds(R.drawable.g_pencil, 0, 0, 0);
					}
					else{
						curate_reject.setCompoundDrawablesWithIntrinsicBounds(R.drawable.g_cross, 0, 0, 0);
					}*/
					onBackPressed();
					alertDialog.dismiss();
				}
			});
			// Showing Alert Message
			alertDialog.show();
			alertDialog.setCanceledOnTouchOutside(false);
		}
	}

	
	
	
	
	
}
